package com.gyms.view.bottomview;

import android.content.Context;
import android.os.Handler;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.classic.okhttp.beans.HVVenueOrderFieldBean;
import com.classic.okhttp.zmoumall.utils.ToastUtil;
import com.gyms.R;
import com.gyms.activity.OrderDetailActivity;
import com.gyms.adapter.RefundAdapter;
import com.gyms.manager.RevertManger;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import constant.EventConstant;
import utils.DecimalUtils;
import utils.StringUtils;
import weight.UiEvent;

/**
 * 退款框
 * ltx
 */
public class RefundDialog extends BaseBottomDialog implements View.OnClickListener, RefundAdapter.ChangePrice {
    private ImageView mTvClose;//关闭
    private TextView mTvDialogTitle;//dialog标题
    private ListView mLvRefund;
    private TextView mTvTotalPrice;//退款价格
    private Button mBtnConfirm;//确认退款
    public double price;
    private Context mContext;
    private RefundAdapter mRefundAdapter;//退单的适配器
    private ArrayList<HVVenueOrderFieldBean> orderField;//子订单列表
    private List<HVVenueOrderFieldBean> mSelectedRefund=new ArrayList<>();
    private Handler mHandler;
    private ApplyRefund mApplyRefund;//退款的回调

    private LinearLayout mLLShowPayWithCard;

    private TextView mTvCardDis;//优惠卷抵扣的价格
    private TextView mTvActrualPay;//实际支付
    private TextView mTvRevertDis;//退款扣减
    private TextView mTvAllPrice;//合计

    private double mActrualPrice;//实际支付价格
    private double mCardDis;//抵扣券抵扣的价格
    private double mAllPrice;//总价

    private TextView mTvGoRevertDeatil;

    private RevertManger mRevertManger;//退款管理类

    private double mPayWithCardRevertMonery;//使用了优惠券退回的金额

    private OrderDetailActivity mOrderDetailActivity;
    private ImageView mIvRevertIcon;

    private LinearLayout mLLRevertDetail;


    public void setPrcieData(double mActrualPrice,double mCardDis, double mAllPrice){
        this.mActrualPrice=mActrualPrice;
        this.mCardDis=mCardDis;
        this.mAllPrice=mAllPrice;

    }


    private boolean isPayWithCard;//是否是使用了优惠券的场馆订单
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
//            DecimalUtils.setPriceText(mContext, DecimalUtils.doubleToIntOrD(Math.abs(price)), mTvTotalPrice, Constants.PRICE_ACTUAL_SIZE, Constants.YUAN_SIZE);
            mTvTotalPrice.setText(new SpannableStringBuilder("退款：").append(DecimalUtils.setPriceStyle(mContext,Math.abs(price),R.dimen.price_text_size_21,R.dimen.price_text_size_18,true)));
        }
    };

    public void setApplyRefund(ApplyRefund applyRefund) {
        mApplyRefund = applyRefund;
    }

    public void setPayWithCard(boolean payWithCard) {
        isPayWithCard = payWithCard;
    }

    public interface ApplyRefund {
        void doRefund();
    }

    public RefundDialog(Context context, int animationStyles, Handler mHandler) {
        super(context, R.style.BottomViewTheme_Defalut);
        this.mContext = context;
        this.mHandler = mHandler;
        animationStyle = animationStyles;
        mOrderDetailActivity= (OrderDetailActivity) context;
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_refund;
    }

    @Override
    protected void initView() {
        findView();
        setListen();
        setAdapter();
        if(isPayWithCard){
            mLvRefund.setVisibility(View.GONE);
            mTvDialogTitle.setText("申请退款");
            mLLShowPayWithCard.setVisibility(View.VISIBLE);
            mTvActrualPay.setText(DecimalUtils.setPriceStyle(mContext,mActrualPrice,R.dimen.price_text_size_14,R.dimen.price_text_size_12,true));
            mTvCardDis.setText(new SpannableStringBuilder("-").append(DecimalUtils.setPriceStyle(mContext,mCardDis,R.dimen.price_text_size_15,R.dimen.price_text_size_13,true)));
            mTvAllPrice.setText(DecimalUtils.setPriceStyle(mContext,mAllPrice,R.dimen.price_text_size_12,R.dimen.price_text_size_10,true));
            mTvRevertDis.setText(new SpannableStringBuilder("-").append(DecimalUtils.setPriceStyle(mContext,mActrualPrice-mPayWithCardRevertMonery,R.dimen.price_text_size_14,R.dimen.price_text_size_12,true)));//退款扣减
            price=mPayWithCardRevertMonery;

        }else{
            mTvDialogTitle.setText("请选择退款项");
            mLvRefund.setVisibility(View.VISIBLE);
            mLLShowPayWithCard.setVisibility(View.GONE);
            //默认初始化价格
//        DecimalUtils.setPriceText(mContext, DecimalUtils.doubleToIntOrD(price), mTvTotalPrice, Constants.PRICE_ACTUAL_SIZE, Constants.YUAN_SIZE);

        }
        mTvTotalPrice.setText(DecimalUtils.setPriceStyle(mContext,price,R.dimen.price_text_size_21,R.dimen.price_text_size_18,true));
        mOrderDetailActivity.doShowRevertTime(mTvGoRevertDeatil, mIvRevertIcon,mOrderDetailActivity.isProduct);
    }

    /**
     * 设置监听
     */

    private void setListen() {
        mTvClose.setOnClickListener(this);
        mBtnConfirm.setOnClickListener(this);
//        mTvGoRevertDeatil.setOnClickListener(this);
        mLLRevertDetail.setOnClickListener(this);
    }

    /**
     * 初始化listview
     */

    private void setAdapter() {
        mRefundAdapter = new RefundAdapter(mContext);
        mLvRefund.setAdapter(mRefundAdapter);
        mRefundAdapter.setChangePrice(this);
    }

    /**
     * 初始控件
     */

    private void findView() {
        mTvClose = (ImageView) findViewById(R.id.tv_close);
        mTvDialogTitle = (TextView) findViewById(R.id.tv_dialog_title);
        mLvRefund = (ListView) findViewById(R.id.lv_refund);
        mTvTotalPrice = (TextView) findViewById(R.id.tv_total_price_refund);
        mBtnConfirm = (Button) findViewById(R.id.btn_confirm);
        mLLShowPayWithCard= (LinearLayout) findViewById(R.id.ll_show_pay_with_card);


        mTvCardDis= (TextView) findViewById(R.id.tv_card_discout);
        mTvActrualPay= (TextView) findViewById(R.id.tv_actrual_pay);
        mTvRevertDis= (TextView) findViewById(R.id.tv_revert_dis);
        mTvGoRevertDeatil= (TextView) findViewById(R.id.tv_go_revert_detail);
        mTvAllPrice= (TextView) findViewById(R.id.tv_all_price);


        mIvRevertIcon= (ImageView) findViewById(R.id.iv_revert_icon);
        mLLRevertDetail= (LinearLayout) findViewById(R.id.ll_revert_rule);


    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_close://关闭按钮
                dismiss();
                break;
            case R.id.btn_confirm://确认退款
                if (StringUtils.isNull(mSelectedRefund)&&!isPayWithCard) {//如果选择的退单的list为null 就返回
                    ToastUtil.showToast(mContext, "请选择需要退款的项目");
                    return;
                }
                ToastUtil.showToast(mContext, "正在退款");
                dismiss();
                if(isPayWithCard){

                    mSelectedRefund.addAll(((OrderDetailActivity)mContext).orderField);
                }
                //退款的回调
                EventBus.getDefault().post(new UiEvent(EventConstant.FLAG_SEAT_REFUND, mSelectedRefund));
                break;
           /* case R.id.tv_go_revert_detail:
               *//* Bundle mBundle = new Bundle();
                mBundle.putBoolean(Constants.GO_RULE_DETAIL,true);
                mBundle.putString(Constants.WEBVIEW_TITLE,"退款详情");
                mBundle.putString(Constants.WEBVIEW_URL, Cons.URL_REVERT_RULE);
                mBundle.putSerializable(Constants.WEBVIEW_CONFIGS_LIST,((OrderDetailActivity)mContext).mRefundConfigs);
                ((BaseActivity)mContext).startActivity(WebViewActivity.class,mBundle,false);*//*
                mOrderDetailActivity.goRevertDetail();
;
                break;*/
            case R.id.ll_revert_rule:
                mOrderDetailActivity.goRevertDetail();
                break;
        }
    }


    public void setOrderField(ArrayList<HVVenueOrderFieldBean> orderField) {
        this.orderField = orderField;
        mRefundAdapter.setOrderField(orderField);
        mRefundAdapter.notifyDataSetChanged();
    }

    @Override
    public void refumdPrice(double pirce) {//将dialog的退款价格更新
        this.price = pirce;
        mHandler.post(runnable);
    }

    @Override
    public void refumdList(List<HVVenueOrderFieldBean> mSelectedRefund) {
        this.mSelectedRefund = mSelectedRefund;

    }

    @Override
    public void refefunDisPrice(double mDisPrice) {
        mTvRevertDis.setText(new SpannableStringBuilder("-").append(DecimalUtils.setPriceStyle(mContext,mDisPrice,R.dimen.price_text_size_14,R.dimen.price_text_size_12,true)));//退款扣减

    }

    /**
     * 退款成功后将适配器的价格设为0
     */
    public void setPriceToO() {
        if (null != mRefundAdapter) {
            mRefundAdapter.mAllRefundPrice = 0;
            price = 0;
//            DecimalUtils.setPriceText(mContext, DecimalUtils.doubleToIntOrD(price), mTvTotalPrice, Constants.PRICE_ACTUAL_SIZE, Constants.YUAN_SIZE);
            mTvTotalPrice.setText(new SpannableStringBuilder("退款").append(DecimalUtils.setPriceStyle(mContext,price,R.dimen.price_text_size_21,R.dimen.price_text_size_18,true)));
            if (!StringUtils.isNull(mSelectedRefund)) {
                mSelectedRefund.clear();
            }
        }
    }

    /**
     * 设置可退款的金额
     * @param mPayWithCardRevertMonery
     */

    public void setPayWithCardRevertMonery(double mPayWithCardRevertMonery){
        this.mPayWithCardRevertMonery=mPayWithCardRevertMonery;
    }

    public void initRevertDis(){
        if(!StringUtils.isNull(mTvRevertDis)){
            mTvRevertDis.setText(new SpannableStringBuilder("-").append(DecimalUtils.setPriceStyle(mContext,mActrualPrice-mPayWithCardRevertMonery,R.dimen.price_text_size_15,R.dimen.price_text_size_13,true)));
        }
        if(null!=mRefundAdapter){
            mRefundAdapter.mDisPrice=0.0;
        }
    }

}
